package controllers;

import java.sql.ResultSet;
import play.*;
import play.mvc.*;

import java.util.*;

import models.*;
import play.db.DB;

public class Posts extends Controller {
	
	public static void getPostsOfCurrentUserJSON() {
		List<Post> p = getPostsOfCurrentUser();
		if (p != null) {
			renderJSON(p, new PostSerializer());
		} else {
			renderJSON(false);
		}
	}
	
	public static List<Post> getPostsOfCurrentUser() {
		User currentUser = Login.getCurrentUser();
		if (currentUser != null) {
			return currentUser.getPosts();
		}
		return null;
	}
	
	public static void getPostsByUserId() {
		Long uid = Long.valueOf(params.get("pid"));
		User u = User.findById(uid);
		if(u == null) {
			renderJSON("{error:\"User not found.\"}");
		} else {
			List<Post> ps = u.getPosts();
			if (ps == null) {
				renderJSON("{error:\"User has no posts.\"}");
			} else {
				renderJSON(ps, new PostSerializer());
			}
		}
	}
	
    public static void createPostJSON() {
		String title = null;
		String content = params.get("content");
		Long uid = Long.valueOf(params.get("uid"));
		//Long cid = Long.valueOf(params.get("cid"));
		User u = User.find("byId", uid).first();
		//Circle c = Circle.find("byId", cid).first();
		if (u != null) {
			Post p = new Post(u, title, content);
			//p.notifiedCircle.add(c);
			p.save();
			renderJSON(p, new PostSerializer());
		} else {
			renderJSON(false);
		}		
	}
	
	public static void createPost() {
		String title = null;
		String content = params.get("content");
		Long uid = Long.valueOf(params.get("uid"));
		String people = params.get("people");
		String circle = params.get("circle");
		User u = User.find("byId", uid).first();
		if (u != null) {
			boolean np = (people.equals("all")) ? true : false;
			Post p = new Post(u, title, content, np);
			if (circle != null) {
				String[] cid = circle.split(",");
				for (int i = 0;i < cid.length; i++) {
					Circle c = Circle.find("byId", cid[i]).first();
					p.notifiedCircles.add(c);
				}
			} else {
				p.notifiedCircles.add(null);
			}
			p.save();
			renderJSON(p, new PostSerializer());
		}
		else {
			renderJSON(false);
		}
	}
	
	public static void deletePost() {
		Long pid = Long.valueOf(params.get("pid"));
		System.out.println(params.get("pid"));
		int res = Post.delete("id=?", pid);
		if(res == 1) {
			renderJSON(pid);
		} else {
			renderJSON(false);
		}
		
	}
	
	public static void getPostsByCircleId(Long cid) {
		if(cid < 0) {
			renderJSON("{}");
		} else {
			List<Post> postsOfCircles = Post.find("select p from Post p, IN(p.notifiedCircles) as c where ?=c.id", cid).fetch();
			renderJSON(postsOfCircles, new PostSerializer());
		}
	}

	public static void getFriendsPostsByUserId(Long uid) throws Exception{
		if(uid < 0) {
			renderJSON("{}");
		} else {
			List<Post> postsOfFriends = Post.find("select p from Post p where p.author.id=? or p.toAll=1 and p.author in (select u from User u, IN(u.friends) as f where ?=f.id) order by p.postDate desc", uid, uid).fetch();
			/*ResultSet resultset = DB.executeQuery("select p from Post p, User_User uu where uu.beFriendOf_id=" + uid + " and uu.friends_id=p.author_id");
			List<Post> postsOfFriends = new ArrayList<Post>();
			while(resultset.next()){
				postsOfFriends.add( new Post( resultset.getString("make"),
						resultset.getString("model"),
						resultset.getString("vin") ) );
			}*/
			
			renderJSON(postsOfFriends, new PostSerializer());
		}
	}
}

